/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.util;

import java.io.IOException;
import java.io.InputStream;

/**
 * This class implements an input stream whose data source may be redirected,
 * that is changed.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.1
 * @since 1.1
 */
public final class RedirectedInputStream extends InputStream {

    /** Current data source. */
    private InputStream current;

    /**
     * Constructs a redirectable input stream from any input stream.
     * @param in data source - should not be <tt>null</tt>
     */
    public RedirectedInputStream(final InputStream in) {
        assert in != null : "null in";
        this.current = in;
    } // end constructor(InputStream)

    /**
     * Changes data source.
     * @param in new data source - should not be <tt>null</tt>
     */
    public void redirect(final InputStream in) {
        assert in != null : "null in";
        this.current = in;
    } // end method 'redirect(InputStream)'

    /**
     * {@inheritDoc}
     */
    @Override
    public int read() throws IOException {
        return this.current.read();
    } // end method 'read()'

} // end class 'RedirectedInputStream'
